home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / Drtf / DNetHTMLHandler.cpp < prev    next >
Encoding:
Text File  |  1995-12-17  |  7.6 KB  |  284 lines  |  [TEXT/R*ch]

  1. // DNetHTMLHandler.cpp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "Dvibrant.h"
  6. #include "DFile.h"
  7. #include "DRichProcess.h"
  8. #include "DRichViewNu.h"
  9. #include "DHTMLprocess.h"
  10.  
  11. #include "DGopher.h"
  12. #include "DGoList.h"
  13. #include "DGoPlus.h"
  14. #include "DGoClasses.h"
  15. #include "DGoInit.h"
  16. #include "DURL.h"
  17.  
  18. #include "DNetHTMLHandler.h"
  19.  
  20.  
  21. class DNetHTMLprocess : public DHTMLprocess
  22. {
  23. public:
  24.     DGopherList * fGolist;
  25.     
  26.     DNetHTMLprocess( DFile* itsFile, DRichView* itsDoc, Nlm_MonitorPtr progress,
  27.                                         DGopherList* itsLinks);
  28.  
  29. protected:
  30.     virtual void    handleEndControl( short attr);  
  31. };
  32.  
  33.  
  34. static unsigned long gLinkcolor = (0<<16) + (170<<8) + 0;
  35.                                                                 // red     green    blue
  36.  
  37.  
  38.  
  39. //class  DNetHTMLHandler
  40.  
  41.  
  42. DNetHTMLHandler::DNetHTMLHandler( DRichView* itsDoc, DFile* savefile, DGopherList * itsList) :
  43.     DHTMLHandler( itsDoc, savefile),
  44.     fGolist(itsList)
  45. {
  46. }
  47.     
  48.          
  49.  
  50. DRichprocess* DNetHTMLHandler::NewProcessor( DFile* itsFile, Nlm_MonitorPtr progress)
  51. {
  52.     fProcessor = new DNetHTMLprocess( itsFile, fDoc, progress, this->fGolist);
  53.     return fProcessor;
  54. }
  55.  
  56.  
  57.  
  58. // class DNetHTMLprocess
  59.  
  60. DNetHTMLprocess::DNetHTMLprocess( DFile* itsFile, DRichView* itsDoc, Nlm_MonitorPtr progress,
  61.                                         DGopherList* itsLinks) :
  62.     DHTMLprocess( itsFile,  itsDoc, progress),
  63.     fGolist(itsLinks)
  64. }
  65.  
  66.  
  67.  
  68.  
  69. void DNetHTMLprocess::handleEndControl(short attr)
  70. {
  71.     // e.g, > end of any control
  72.  
  73.     if (fInForm && (fCurControl == htmlForm 
  74.      || fCurControl == htmlTextarea 
  75.      || fCurControl == htmlSelect 
  76.      || fCurControl == htmlInput)) {
  77.         // if fCurControl == htmlForm) need to save fURLstore, as it is the url for the form ...
  78.         // this comes at start of form block
  79.         // need to process later EndControls as part of this form until
  80.         // fInForm is set false.
  81.         if (fCurControl == htmlForm) {
  82.             MemFree( fFormURL);
  83.             fFormURL= fURLstore;
  84.             fURLstore= NULL;
  85.             DHTMLprocess::handleEndControl( attr); // this handles image stuff !?
  86.             }
  87.         else {
  88.             //this->handleLinkAttr( fCurControl); //!! attr == -1 here !
  89.             // this is on the tricky side, but lets us trap text outside of the <control>
  90.             // for use in the form label...
  91.             fInControl= true;
  92.             fQuote = (char)234; // kInFormText
  93.             // DHTMLprocess::handleEndControl( attr); // this handles image stuff !?
  94.             }
  95.         return;
  96.         }
  97.  
  98. // !!! MUST DISTINGUISH fURLstore for LINK (anchor) && fURLstore for picture !!
  99. // picture can be inside of a link (& clickable for that link)
  100. // !!! when an image is linked it is inside htmlAnchor
  101. //     currently htmlImage inside Anchor wipes out anchor link info !
  102. //     need to push anchor link &/or tie it in to image link !
  103.  
  104.     if (true && (  // if (fURLstore && ...)
  105.      fCurControl == htmlAnchor || 
  106.      fCurControl == htmlImage || 
  107.      fCurControl == htmlForm)) {
  108.       
  109.         if (fCurControl == htmlForm) {
  110.             MemFree(fURLstore);
  111.             fURLstore= fFormURL;
  112.             fFormURL= NULL;
  113.             }
  114.             
  115.       DGopher* go= NULL;
  116.       ulong urlsize= StringLen(fURLstore);      
  117.         if (true) { // if (urlsize)
  118.             char* url = NULL;
  119.             char* ref = NULL;
  120.             Boolean haveIndexItem = false;
  121.             
  122.             ref = StringChr(fURLstore,'#');
  123.             if (ref) {
  124.                         // ref is stored as gopher fQueryGiven field -- ??
  125.                 *ref++= 0;
  126.                 ref= StrDup(ref);
  127.                 urlsize= StrLen(fURLstore);
  128.                 }
  129.                 
  130.             if ((!urlsize || fIsIndex && StringCmp(fURLstore,"?")==0) && fGolist->fParentMenu) {
  131.                 // refer to local/base url
  132.                 MemFree(fURLstore);
  133.                 fURLstore= StrDup( fGolist->fParentMenu->GetURL());
  134.                 urlsize= StrLen(fURLstore);
  135.                 haveIndexItem= true;
  136.                 }
  137.                 
  138.             else if (DURL::IsURL( fURLstore, url, urlsize) == DGopher::kUnknownProt) {
  139.                 if (StrStr(fURLstore,"://")==NULL && fGolist->fParentMenu) {
  140. #if 0
  141.                     char *cp, *ep, *path;
  142.                     
  143.                     if (*fURLstore == '"' || *fURLstore == '\'') {
  144.                         cp= fURLstore + 1;
  145.                         ep= StrChr( cp, *fURLstore);
  146.                         if (ep) *ep= 0;
  147.                         MemMove( fURLstore, cp, StrLen(cp)+1);
  148.                         }
  149.                     
  150.                     path= StrDup( fGolist->fParentMenu->GetURL());
  151.                     cp= StrRChr( path, '/');
  152.                     if (cp) { cp++; *cp= 0; }
  153.                     StrPrependCat( &fURLstore, path);
  154.                     MemFree( path);
  155. #else
  156.                     const char * baseurl= fGolist->fParentMenu->GetURL();
  157.                     char * parturl= NULL;
  158.                     char *cp, *ep;
  159.  
  160.                     parturl= DURL::GetParts(baseurl, 
  161.                             DURL::kPartProtocol +DURL::kPartHost +DURL::kPartPort);
  162.                             
  163.                         // !? need to drop " or ' from fURLstore for use with GetParts...
  164.                     if (*fURLstore == '"' || *fURLstore == '\'') {
  165.                         cp= fURLstore + 1;
  166.                         ep= StrChr( cp, *fURLstore);
  167.                         if (ep) *ep= 0;
  168.                         MemMove( fURLstore, cp, StrLen(cp)+1);
  169.                         }
  170.                         
  171.                         // if (fURLstore is relative to base path...) 
  172.                         // ?? is this bizarre mess really part of html syntax?
  173.                     if (*fURLstore != '/') { 
  174.                         char* pathname= DURL::GetParts(baseurl, DURL::kPartPath);
  175.                         char* path = pathname;
  176.                         cp= StrRChr( pathname, '/');
  177.                         if (cp) { cp++; *cp= 0; }
  178.                         else path= "";
  179.                         StrPrependCat( &fURLstore, path);
  180.                         MemFree(pathname);
  181.                         }
  182.                     else {
  183.                         long lenpart= StrLen(parturl)-1;
  184.                         if (parturl[lenpart] == '/') parturl[lenpart]= 0;
  185.                         }
  186.                     StrPrependCat(&fURLstore, parturl);
  187.                     MemFree(parturl);
  188. #endif                    
  189.                     } 
  190.                 urlsize= StrLen(fURLstore);
  191.                 }
  192.                 
  193.             char gotype;
  194.             if (fCurControl == htmlImage) gotype= kTypeImage;
  195.             else if (haveIndexItem) gotype= kTypeQuery; //??!! this also for Forms?
  196.             else gotype= 0;
  197.  
  198.           if (fGolist && urlsize) 
  199.               go= DGopherList::GopherFromURL( fURLstore, urlsize, fGolist, false, gotype); 
  200.             if (go) {
  201.             
  202.                 if (fCurControl == htmlImage) {
  203.                         // must we infer image type from path suffix ??
  204.                     go->AddView("image/gif"); // what about image/jpg & others ??
  205.                     if (fIsMap) go->fIsMap= true;
  206.                     }
  207.                     
  208.                 else if (fCurControl == htmlForm) {  
  209.                     char hasaskform= 3;  
  210.                     if (StrICmp(fMethodstore, "POST")==0) hasaskform= 5;
  211.                     go->SetPlusInfo( kGopherPlusYes, hasaskform, fGoplusStore);
  212.                     }
  213.                 // else go->AddView("text/html"); // not always !? gopher links, etc
  214.  
  215.                     // screen out duplicate links
  216.                 short linkid= kEmptyIndex;
  217. #if 1
  218.                 {
  219.                 short i, n= fGolist->GetSize();
  220.                 for (i=0; i<n; i++) {
  221.                     if ( go->Equals( fGolist->GopherAt(i)) ) {
  222.                         linkid= i;
  223.                         break;
  224.                         }
  225.                     }
  226.                 }
  227. #else
  228.                     // this isn't catching dups !
  229.                 linkid= fGolist->GetEqualItemNo( (DObject*) go);    
  230. #endif
  231.  
  232.                 if (linkid == kEmptyIndex) {
  233.                     fGolist->InsertLast(go);
  234.                     linkid= fGolist->GetSize(); // note this is +1 of real list index
  235.                     }
  236.                 else linkid++; // +1 !
  237.                 if (fNamestore) go->StoreName(fNamestore);    // THIS was BAD !!?!?!
  238.                 if (ref) go->fQueryGiven= StrDup(ref);
  239.                 
  240.                 if (fCurControl == htmlImage) {
  241.                     go->fTransferType= kTransferBinary;
  242.                     fPicLink= linkid; //1 + fGolist->GetIdentityItemNo( (DObject*) go);    
  243.                     }
  244.                 else {
  245.                         
  246.                         // !? if (fCurControl == htmlForm)  //?? add some "Click here for Form" stuff?
  247.                         
  248.                     StoreStyle(fOldStyleRec.style, FALSE);  // required !
  249.                     fStyleRec.style.color= gLinkcolor;
  250.                     fStyleRec.style.linkid= linkid; //1 + fGolist->GetIdentityItemNo( (DObject*) go);    
  251.                     PutLitChar(' '); // ?? need for link style            
  252.                     StoreStyle(fStyleRec.style, TRUE); 
  253.                     fAnchorLink= linkid; 
  254. #if 0 
  255.                         // we are missing links on case where other styles appear before </a>
  256.                         // try w/ linkid in style until popped by </a>
  257.                     fStyleRec.style.color= 0;
  258.                     fStyleRec.style.linkid= 0;                
  259. #endif
  260. #if 1
  261.                     if (fCurControl == htmlForm) {  //?? add some "Click here for Form" stuff?
  262.                         MemFree(fPushBufstore);
  263.                         fPushBufstore= StrDup("<P> Click HERE to fill out form</A><P>");
  264.                         fPushBuf= fPushBufstore;
  265.                         fPushSize= StrLen(fPushBuf);
  266.                         }
  267. #endif
  268.                     }
  269.                 
  270.                 }
  271.             MemFree( ref);
  272.             }
  273.  
  274.         fURLstore= (char*) MemFree(fURLstore);
  275.         }
  276.  
  277.      DHTMLprocess::handleEndControl( attr); // this handles image stuff !?
  278.          
  279. }
  280.  
  281.  
  282.  
  283.